package net.bingosoft.common.service;

import org.springframework.aop.Advisor;
import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;

/**
 * 2008-5-12 下午12:50:15
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:自动为以Service结尾的类代理事务
 * ChangeLog:
 */
public class AutoServiceFactoryBean extends BeanNameAutoProxyCreator {
	

	public AutoServiceFactoryBean() {
		super();
		setProxyTargetClass(true);
		setBeanNames(null);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void setBeanNames(String[] beanNames) {
		if(beanNames==null || beanNames.length == 0){
			super.setBeanNames(new String[]{"*Service"});
		}else{
			super.setBeanNames(beanNames);
		}
	}
	/**
	 * Determine the advisors for the given bean, including the specific interceptors
	 * as well as the common interceptor, all adapted to the Advisor interface.
	 * @param beanName the name of the bean
	 * @param specificInterceptors the set of interceptors that is
	 * specific to this bean (may be empty, but not null)
	 * @return the list of Advisors for the given bean
	 */
	protected Advisor[] buildAdvisors(String beanName, Object[] specificInterceptors) {
		// Handle prototypes correctly...
		Advisor[] advisors = super.buildAdvisors(beanName, specificInterceptors);
		if(advisors == null || advisors.length == 0){
			Advisor transactionAdvisor = GlobalAdvisorAdapterRegistry.getInstance().wrap(new ServiceTransactionInterceptor());
			advisors = new Advisor[]{transactionAdvisor};
		}
		return advisors;
	}

}
